package edu.ccut.saturn.studio.logic.parts;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;

import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.BasicShape;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.ShapesDiagram;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnFalseConnection;

public class ShapesEditPartFactory implements EditPartFactory {

	private static final String SHAPES_EDIT_PART_FACTORY_NULL1 = "null";
	private static final String SHAPES_EDIT_PART_FACTORY_ERROR = "Can't create part for model element: ";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart,
	 *      java.lang.Object)
	 */
	public EditPart createEditPart(EditPart context, Object modelElement) {
		// get EditPart for model element
		EditPart part = getPartForElement(modelElement);
		// store model element in EditPart
		part.setModel(modelElement);
		return part;
	}

	/**
	 * Maps an object to an EditPart.
	 * 
	 * @throws RuntimeException
	 *             if no match was found (programming error)
	 */
	private EditPart getPartForElement(Object modelElement) {
		if (modelElement instanceof ShapesDiagram) {
			DiagramEditPart diagramEditPart = new DiagramEditPart();
			((BasicShape) modelElement).setEditPart(diagramEditPart);
			return diagramEditPart;
		}

		if (modelElement instanceof Shape) {
			
			return new ShapeEditPart();
		}
		if (modelElement instanceof SaturnFalseConnection) {
			return new SaturnFalseConnectionEditPart();
		}
		if (modelElement instanceof Connection) {
			return new ConnectionEditPart();
		}
		throw new RuntimeException(SHAPES_EDIT_PART_FACTORY_ERROR
				+ ((modelElement != null) ? modelElement.getClass().getName()
						: SHAPES_EDIT_PART_FACTORY_NULL1));
	}

}